package com.bigmi.admin.interceptor;

import com.bigmi.model.admin.pojo.AdUser;
import com.bigmi.utils.thread.AdThreadLocalUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AdTokenInterceptor implements HandlerInterceptor {
    //前置

    /**
     * 得到header中的用户信息，存入当前线程
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String id = request.getHeader("id");
        //判断id是否为空
        if (id != null){
            //存入线程
            AdUser adUser =new AdUser();
            adUser.setId(Integer.valueOf(id));
            AdThreadLocalUtil.setUser(adUser);
        }
        return true;
    }
    //后置

    /**
     * 清理线程中的数据
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        AdThreadLocalUtil.clear();
    }
}
